﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace WikiDoc.Lines
{
    class CodeLine : WikiLine
    {
        public string Content { get; private set; }

        public string Tag { get; private set; }

        protected CodeLine(string line, string tag, string content) : base(line, LineTypes.CodeTag)
        {
            Tag = tag;
            Content = content;
        }

        /// <summary>
        /// 用户自己输入的HTML块文本标签
        /// </summary>
        public static readonly Regex Pattern = new Regex(
            @"\A \s*  <([^>]+)> (.*) \z",
            RegexOptions.Compiled |
            RegexOptions.CultureInvariant |
            RegexOptions.Singleline |
            RegexOptions.IgnorePatternWhitespace);

        public static CodeLine TryParse(string line)
        {
            if (string.IsNullOrEmpty(line)) return null;

            var match = Pattern.Match(line);

            return match.Success && !match.Groups[1].Value.EndsWith("/")
                       ? new CodeLine(line, match.Groups[1].Value, match.Groups[2].Value)
                       : null;
        }

        ///// <summary>
        ///// 用户输入的标签结束，必须在行尾
        ///// </summary>
        //public static readonly Regex EndPattern = new Regex(
        //    @"\A (?<content> .*)  </code> (\s*) \z",
        //    RegexOptions.Compiled |
        //    RegexOptions.CultureInvariant |
        //    RegexOptions.Singleline |
        //    RegexOptions.IgnorePatternWhitespace);
    }
}
